package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.contant.model.dto.CourseTeacherDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseTeacherConvert;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Override
    public void removeCourseTeacher(Long courseBaseId, Long teacherId, Long companyId) {
        //1.判断关键数据
        if (ObjectUtils.isEmpty(courseBaseId)) {
            ExceptionCast.cast(CommonErrorCode.E_110006);
        }
        if (ObjectUtils.isEmpty(teacherId)) {
            ExceptionCast.cast(CommonErrorCode.E_110006);
        }
        if (ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }
        //2.判断业务数据
        CourseTeacher courseTeacher = this.getById(teacherId);
        if (ObjectUtils.isEmpty(courseTeacher)){
            ExceptionCast.cast(ContentErrorCode.E_1200503);
        }
        if (!courseBaseId.equals(courseTeacher.getCourseId())){
            ExceptionCast.cast(ContentErrorCode.E_120019);
        }
        //3.删除数据
        boolean remove = this.removeById(teacherId);
        if (!remove){
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }

    }


    /**
     * 添加课程教师基本信息
     * @param dto 课程教师信息dto封装类
     * @return 返回DTO数据给前端
     */
    @Override
    @Transactional
    public CourseTeacherDTO createOrModifyCourseTeacher(CourseTeacherDTO dto,Long companyId) {
        CourseTeacherDTO courseTeacherDTO = new CourseTeacherDTO();
        if (ObjectUtils.isEmpty(dto.getId())) {
            courseTeacherDTO = createTeacher(dto, companyId);
        } else {
            courseTeacherDTO = updateTeacher(dto, companyId);
        }
        return courseTeacherDTO;
    }

    // 修改
    private CourseTeacherDTO updateTeacher(CourseTeacherDTO dto, Long companyId) {
        //1.判断关键数据
        if (ObjectUtils.isEmpty(dto)) {
            ExceptionCast.cast(CommonErrorCode.E_110006);
        }
        if (ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }
        if (ObjectUtils.isEmpty(dto.getCourseId())) {
            ExceptionCast.cast(ContentErrorCode.E_120018);
        }
        if (ObjectUtils.isEmpty(dto.getTeacherName())) {
            ExceptionCast.cast(ContentErrorCode.E_1200503);
        }
        if (ObjectUtils.isEmpty(dto.getPosition())) {
            ExceptionCast.cast(ContentErrorCode.E_1200503);
        }
        CourseTeacher courseTeacher = this.getById(dto.getId());
        if (ObjectUtils.isEmpty(courseTeacher)) {
            ExceptionCast.cast(ContentErrorCode.E_1200503);
        }
        if (!courseTeacher.getCourseId().equals(dto.getCourseId())) {
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }
        courseTeacher.setCourseId(dto.getCourseId());
        courseTeacher.setTeacherName(dto.getTeacherName());
        courseTeacher.setIntroduction(dto.getIntroduction());
        courseTeacher.setPhotograph(dto.getPhotograph());
        courseTeacher.setPosition(dto.getPosition());
        boolean update = this.updateById(courseTeacher);
        if (!update) {
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }
        CourseTeacher po = this.getById(dto.getId());
        CourseTeacherDTO courseTeacherDTO = CourseTeacherConvert.INSTANCE.entity2dto(po);
        return courseTeacherDTO;
    }
    // 新增
    private CourseTeacherDTO createTeacher(CourseTeacherDTO dto, Long companyId) {
        //1.判断关键数据
        if (ObjectUtils.isEmpty(dto)) {
            ExceptionCast.cast(CommonErrorCode.E_110006);
        }
        if (ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }
        if (ObjectUtils.isEmpty(dto.getCourseId())) {
            ExceptionCast.cast(ContentErrorCode.E_120018);
        }
        if (ObjectUtils.isEmpty(dto.getTeacherName())) {
            ExceptionCast.cast(ContentErrorCode.E_1200503);
        }
        if (ObjectUtils.isEmpty(dto.getPosition())) {
            ExceptionCast.cast(ContentErrorCode.E_1200503);
        }
        // 数据转换 dto 转 po , 获取课程教师对象
        CourseTeacher courseTeacher = CourseTeacherConvert.INSTANCE.dto2po(dto);
        //给课程教师表赋值
        courseTeacher.setCourseId(dto.getCourseId());
        courseTeacher.setIntroduction(dto.getIntroduction());
        courseTeacher.setPhotograph(dto.getPhotograph());
        courseTeacher.setPosition(dto.getPosition());
        courseTeacher.setTeacherName(dto.getTeacherName());
        // 保存到数据库
        boolean flag = this.save(courseTeacher);
        if (!flag){
            ExceptionCast.cast(ContentErrorCode.E_1200501);
        }
        // 获取当前最新数据
        CourseTeacher po = this.getById(courseTeacher.getId());
        // 将数据库最新的数据转换为dto并返回
        CourseTeacherDTO courseTeacherDTO = CourseTeacherConvert.INSTANCE.entity2dto(po);
        // 返回数据
        return courseTeacherDTO;
    }



    /**
     * 根据课程信息id查询课程教师信息
     * @param id 课程基本信息id
     * @return 返回数据 dto
     */
    @Override
    public List<CourseTeacher> queryCourseTeacherById(Long id, Long companyId) {
        // 关键数据判断         业务判断
        if (ObjectUtils.isEmpty(id)){
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }

        if (ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }
        // 根据课程信息id查询
        LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseTeacher::getCourseId,id);
        List<CourseTeacher> list = this.list(wrapper);
        return list;
    }
}
